# _*_coding     : UTF_8_*_
# Author        :Jie Shen
# CreatTime     :2022/2/15 20:20

from jshen.spider import jselenium, sleep
# 若没有jshen文件夹，可以 pip install jshen
class qcc_test(jselenium):
    def __init__(self, driver_path):
        super(qcc_test, self).__init__(driver_path)

    def open_qcc(self):
        self.driver.get("https://www.qcc.com")
        q.driver.maximize_window()
        sleep()
        win_str = "https://www.qcc.com/web/search/advance"
        self.driver.get(win_str)

    def check_high_tech_enterprise(self):
        """
        """
        self.main_windows_scroll(10000)

        Start = "//span[text()=\" 高新企业 \"]"
        self.hover_by_xpath(Start)

        xpath_str = "//a[@title=\"高新技术企业\"]"
        self.click_by_xpath(xpath_str)

        # 回到页面顶部
        self.main_windows_scroll(0)

    def click_shanxi(self):
        """
        以查询山西省的高新企业数为例子
        :return:
        """
        # 悬停在 省份地区 ——> 全部地区
        xpath_str = "/html/body/div[1]/div[2]/div[1]/div/div[1]/div/" \
                    "div[2]/div[4]/div[4]/div[2]/div/div/span/a/span"
        self.hover_by_xpath(xpath_str)

        # 悬停在第一个省
        xpath_str = "/html/body/div[1]/div[2]/div[1]/div/div[1]/div/div[2]/" \
                    "div[4]/div[4]/div[2]/div/div/div/div/div[1]/div/ul/li[1]"
        self.hover_by_xpath(xpath_str)

        # 省的窗口下滑
        go = True
        p_offset = 0
        while go and p_offset<4000:
            if p_offset > 0:
                js = f'document.getElementsByClassName("drop-col")[3].scrollTop={p_offset}'
                self.driver.execute_script(js)
            sleep()
            p_offset += 100
            p_xpath_str= "/html/body/div/div[2]/div[1]/div/div[1]/div/div[2]/div[4]/div[4]/div[2]/div/div/div/div/div[1]/div/ul"
            provience_window = self.driver.find_element_by_xpath(p_xpath_str)
            selector = provience_window.find_elements_by_css_selector("li")
            for li in selector:
                text = li.find_element_by_xpath("./a/label/span").text.strip()
                if text == "山西省":
                    shanxi_xpath = "//span[text()='山西省']"
                    self.hover_by_xpath(shanxi_xpath)
                    self.click_by_xpath(shanxi_xpath)
                    go = False
                    break
        num_xpath = "/html/body/div/div[2]/div[4]/div/span/span"
        nums = self.driver.find_element_by_xpath(num_xpath).text
        print(f"山西省的高新企业总数是{nums}")



if __name__ == '__main__':
    driver_path = r"D:\1_git\spider\selenium\driver\chromedriver.exe"
    q = qcc_test(driver_path)
    q.open_qcc()
    q.check_high_tech_enterprise()
    q.click_shanxi()
